package classes.controlers {
	import adobe.utils.CustomActions;
	import classes.managers.Game_Manager;
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Timer_Controler extends A_Controler {
		
		private var time:int = 0;
		private var milSec:int = 0;
		private var milSecTimer:Timer = new Timer( 1 );
		private var timer:Timer = new Timer( 1000 );
		
		private var level:int = 1;
		
		private var _day:String = "00";
		private var _hour:String = "00";
		private var _min:String = "00";
		private var _sec:String = "00";
		private var _milSecond:String = "00";
		
		private var isStart:Boolean = false;
		
		public function Timer_Controler( _gm:Game_Manager, _mc:MovieClip ) {
			super( _gm, _mc );
			
			addEvent();
		}
		
		public function init():void {
			time = 0;
			milSec = 0;
			isStart = false;
			
			level = 1;
			day = "00";
			hour = "00";
			min = "00";
			sec = "00";
			milSecond = "00";
			
			mc.milSec_txt.text = makeMilsec();
			mc.sec_txt.text = zeroFill( time );
			mc.min_txt.text = zeroFill( time );
			mc.hour_txt.text = zeroFill( time );
			mc.day_txt.text = zeroFill( time );
			
			milSecTimer.reset();
			timer.reset();
		}
		
		public function start():void {
			if ( isStart === false ) {
				milSecTimer.start();
				timer.start();
				isStart = true;
			}
		}

		public function stop():void {
			milSecTimer.stop();
			timer.stop();
		}
		
		private function addEvent():void {
			milSecTimer.addEventListener(TimerEvent.TIMER, milSecTIMER );
			timer.addEventListener(TimerEvent.TIMER, TIMER );
		}
		
		private function TIMER(e:TimerEvent):void {
			time++;
			
			var _sec:Number = time % 60;
			var _min:int = time / 60;
			var _hour:int = time / 60 / 60;
			var _day:int = time / 60 / 60 / 24;
			
			sec = zeroFill( _sec );
			min = zeroFill( _min );
			hour = zeroFill( _hour );
			day  = zeroFill( _day );
			
			
			mc.sec_txt.text = zeroFill( _sec );
			mc.min_txt.text = zeroFill( _min );
			mc.hour_txt.text = zeroFill( _hour );
			mc.day_txt.text = zeroFill( _day );
			
			diruptStart();
			
			bang();
		}
		
		private function bang():void {
			if ( time <= 60 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 1;
			}else if ( time > 60 && time <= 120 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 2;
			}else if ( time > 120 && time <= 180 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 3;
			}else if ( time > 180 && time <= 300 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 4;
			}else if ( time > 300 && time <= 330 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 5;
			}else if ( time > 330 && time <= 600 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 6;
			}else if ( time > 600 && time <= 615 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 7;
			}else if ( time > 615 ) {
				if ( time % 15 === 0 ) {
					gm.bang(level); 
				}
				level = 8;
			}
		}
		
		// 방해요소 시간.
		private function diruptStart():void {
			switch (time) {
				case 20: gm.disruptStart( 1 );
				break;
				case 40: gm.disruptStart( 2 );
				break;
				case 60: gm.disruptStart( 3 );
				break;
				case 90: gm.disruptStart( 4 );
				break;
				case 120: gm.disruptStart( 5 );
				break;
				case 180: gm.disruptStart( 6 );
				break;
				case 420: gm.disruptStart( 7 );
				break;
				case 900: gm.disruptStart( 8 );
				break;
				case 1800: gm.disruptStart( 9 );
				break;
				case 3600: gm.disruptStart( 10 );
				break;
				default:
			}
		}
		
		private function milSecTIMER(e:TimerEvent):void {
			milSec++;
			mc.milSec_txt.text = makeMilsec();
			
			milSecond = makeMilsec();
		}
		
		private function zeroFill( _num:Number ):String {
			var str:String;
			
			if ( _num < 10 ) {
				str = "0" + String(_num);
			}else if ( _num >= 10 ) {
				str = String( _num );
			}
			
			return str;
		}
		
		private function makeMilsec():String {
			var str:String;
			
			if ( milSec === 100 ) {
				milSec = 0;
			}
			
			if ( milSec < 10 ) {
				str = "0" + milSec;
			}else if ( milSec >= 10 && milSec < 100 ) {
				str = String(milSec).substr(0,1) + String(milSec).substr(1,1);
			}
			
			return str;
		}
		
		public function get day():String 
		{
			return _day;
		}
		
		public function set day(value:String):void 
		{
			_day = value;
		}
		
		public function get hour():String 
		{
			return _hour;
		}
		
		public function set hour(value:String):void 
		{
			_hour = value;
		}
		
		public function get min():String 
		{
			return _min;
		}
		
		public function set min(value:String):void 
		{
			_min = value;
		}
		
		public function get sec():String 
		{
			return _sec;
		}
		
		public function set sec(value:String):void 
		{
			_sec = value;
		}
		
		public function get milSecond():String 
		{
			return _milSecond;
		}
		
		public function set milSecond(value:String):void 
		{
			_milSecond = value;
		}
	}
}